package org.lightrpc.format.json;

import org.lightrpc.Response;
import org.lightrpc.format.SerializationFailedException;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.model.JSONObject;
import com.sdicons.json.model.JSONValue;

public class JsonToolsDataSerializer{


	public Response serialize(Object object)throws SerializationFailedException{
		try {
			JSONValue value = JSONMapper.toJSON( object );
			return new Response(value.render(true));
		} catch (MapperException e) {
			throw new SerializationFailedException("Could not serialize data", e);
		}
	}

	public Object deserialize(Response object, Class<?> clazz) throws SerializationFailedException{
		JSONObject json = new JSONObject();
		json.setData(object);
		Object bean;
		try {
			bean = JSONMapper.toJava( json, clazz );
		} catch (MapperException e) {
			throw new SerializationFailedException("Could not deserialize data", e);
		}
		return bean;
	}


}
